/**
 * 审批页面
 * 跳转时传入 {
 *   approveType:'业务类型', 
 *   dictKey:'业务key',
 *   backPage:'返回页面'
 * }
 */
import React, { Component } from 'react';
import {
	StyleSheet, Text, View, TouchableOpacity,
	PixelRatio, Image, FlatList, TextInput
} from "react-native";
import { NavigationEvents } from 'react-navigation';
import { GLOBAL } from '../../../config/global';
import { style } from '../../../theme/style';
import { Common } from '../../../component/common/Common.js';
import { PageStatusIndex } from '../../../component/page-status/PageStatusIndex';
import { API, API_GENERATE } from '../../../config/api';
import { NewOilApproveItem } from '../_component/NewOilApproveItem';

import HTTP from '../../../utils/http'
const scale = GLOBAL.SCALE;
const searchIcon = require('../../../imgs/global/whiteSearch.png');

export class newOilSellingList extends Component {
	static navigationOptions = ({ navigation, navigationOptions }) => {
		return {
			headerTitle: '新油抛单审批',
			headerTintColor: style.color.white,
			headerLeft: <Common type="headLeft" onPress={() => { navigation.navigate('HomePage') }} color='#fff' />,
			headerRight: (<Image style={style.head.headRightEmpty} source={require('../../../imgs/goBackEmpty.png')} />),
			headerStyle: style.head.headerStyle,
			headerTitleStyle: {
				flex: 1,
				textAlign: 'center'
			}
		}
	};

	constructor(props) {
		super(props);
		this.state = {
			newLotData: [],  // 待审批
			status: 'loading',
			loadIcon: false,  // 下拉涮新的 图标
		}
		this.pageNo = 1;
		this.totalPage = 1;
		this.pageParams = {
			searchCode: null,
		};
	}

	async componentWillMount () {
		await this._getListData();
	}


	//获取待审批,已审批列表
	async _getListData () {
		if (this.pageParams.searchCode === '') {
			this.pageParams.searchCode = null
		}
		this.setState({
			loadIcon: true
		})

		let sendData = {
			pageNo: this.pageNo,
			searchCode: this.pageParams.searchCode,
		}
		let responseData = await HTTP.POST(API_GENERATE(API.MERGE_LIST), sendData)
		if (responseData) {
			this.totalPage = responseData.totalPage;
			if (this.pageNo != 1) {  // 加载
				this.setState({
					newLotData: this.state.newLotData.concat(responseData.rows),
					status: 'data',
					loadIcon: false
				})
			} else { // 刷新
				this.setState({
					newLotData: responseData.rows,
					status: responseData.rows.length ? 'data' : 'empty',
					loadIcon: false
				})
			}
		} else {
			this.setState({
				status: 'error'
			})
		}
	}

	// 下拉更新函数
	_isLoadRefresh () {
		this.pageNo = 1;
		this._getListData();
	}

	//上啦加载
	_loadData () {
		if (this.totalPage > this.pageNo) {
			this.pageNo++;
			this._getListData({
				pageNo: this.pageNo,
				onrefresh: true
			});
		}
	}

	//数据加载出错回调
	_onErrorPress = () => {
		this.setState({
			status: 'loading'
		}, function () {
			this._getListData();
		})
	}
	// 一条数据
	_renderItem(list) {
		return (
			<NewOilApproveItem
				itemInfo={list.item}
				loadRefresh={() => {
					this.pageNo = 1
					this._isLoadRefresh();
				}}
				goDetail={() => {
					this.props.navigation.navigate('newOilSellingDetail', {
						item:list.item,
						callback: () => this._isLoadRefresh()
					})
				}}
			/>
		)
	}

	_getSearchView () {
		/**
		 * 客户列表页隐藏头部，此处 搜索框和添加企业按钮作为头部展示
		 */
		return (
			<View style={styles.searchCell}>
				<TouchableOpacity style={styles.searchInputCell} activeOpacity={1} onPress={() => {
					const { navigate } = this.props.navigation;
					navigate('EnterpriseSearchPage', {
					});
				}}>
					<View style={styles.searchInput}>
						<Image source={searchPic} style={styles.searchPlaceholderPic} />
					</View>
				</TouchableOpacity>
				<TouchableOpacity style={styles.searchLeftCell} activeOpacity={1} onPress={() => {
					const { navigate } = this.props.navigation;
					navigate('EnterpriseCreateBaseInfo');
				}}>
				</TouchableOpacity>
				<TouchableOpacity activeOpacity={1} onPress={() => {
					this.setState({
						filterModalVisible: true,
					});
				}}>
					<Text style={styles.filterText}>筛选</Text>
				</TouchableOpacity>
			</View>
		);
	}

	render () {
		let { companyName, newLotData, status } = this.state;
		// const searchView = this._getSearchView();
		return (
			<View style={styles.container}>
				<View style={styles.filterWrap}>
					<View>
						<Image source={searchIcon} style={styles.searchIcon} />
						<TextInput
							ref="enterpriseInput"
							style={styles.searchInput}
							underlineColorAndroid='transparent'
							placeholder={"请输入品类/销售单号/港口"}
							placeholderTextColor="#fff"
							maxLength={30}
							returnKeyType={'search'}
							defaultValue={this.pageParams.searchCode}
							onChangeText={(txt) => { this.pageParams.searchCode = txt; }}
							onSubmitEditing={() => { this._isLoadRefresh(); }}
						/>
					</View>
				</View>
				<NavigationEvents onWillFocus={async () => {
					await this._getListData()
				}} onDidBlur={() => { this.pageNo = 1; }} />
				{
					status == 'data' ? (
						<View style={styles.listBox}>
							<FlatList
								data={newLotData}
								renderItem={this._renderItem.bind(this)}
								refreshing={this.state.loadIcon}
								onRefresh={this._isLoadRefresh.bind(this)}
								onEndReachedThreshold={0.1}
								onEndReached={() => { this._loadData() }}
							/>
						</View>
					) : null
				}
				<PageStatusIndex status={status} emptyText="暂无记录" onErrorPress={this._onErrorPress} />
			</View >
		)
	}
}
const styles = StyleSheet.create({
	container: {
		flex: 1,
		backgroundColor: style.color.white,
	},
	listItem: {
		width: '100%',
		paddingTop: 12 * scale,
		paddingBottom: 12 * scale,
		flexDirection: 'row',
		borderBottomWidth: 1 / PixelRatio.get(),
		borderStyle: 'solid',
		borderBottomColor: style.color.gray,
	},
	leftImg: {
		width: 40 * scale,
		height: 40 * scale,
		marginLeft: 15 * scale
	},
	conent: {
		width: 265 * scale,
		paddingLeft: 15 * scale
	},
	rightText: {
		color: '#2F7BFF'
	},
	titleBox: {
		flexDirection: 'row',
		alignItems: 'center',
		marginBottom: 5 * scale
	},
	title: {
		fontSize: 14 * scale,
		color: '#222',

	},
	infoText: {
		fontSize: 12 * scale,
		color: '#8b8b8b'
	},
	elementImg: {
		width: 21 * scale,
		height: 14 * scale,
		marginLeft: 5 * scale
	},
	listBox: {
		flex: 1,
		// height: '100%',
		//paddingBottom: 40 * scale,
		backgroundColor: style.color.bgGray,
		paddingHorizontal: 10,
	},
	//列表item样式
	orderText: {
		fontSize: 12 * scale,
		color: '#8B8B8B'
	},
	orderVal: {
		fontSize: 12 * scale,
		color: '#000'
	},
	orderCol: {
		flexDirection: 'row',
		alignItems: 'center',
		height: 25 * scale,
		width: 175.5 * scale,
	},
	orderMain: {
		flexDirection: 'row',
		paddingTop: 8 * scale,
		paddingBottom: 8 * scale,
		flexWrap: 'wrap'
	},
	orderHeadText: {
		fontSize: 14 * scale,
		color: '#222',
		marginRight: 6 * scale,
		marginLeft: 6 * scale,
		flexWrap: 'wrap',
	},
	orderHeadLeft: {
		height: 44 * scale,
		alignItems: 'center',
		flexDirection: 'row',
	},
	orderHead: {
		width: 351 * scale,
		borderBottomColor: '#e4e4e4',
		borderBottomWidth: 1 / PixelRatio.get(),
		height: 44 * scale,
		justifyContent: 'space-between',
		alignItems: 'center',
		flexDirection: 'row',
	},
	orderCell: {
		width: 375 * scale,
		backgroundColor: '#fff',
		alignItems: 'center',
		marginBottom: 12 * scale,
	},
	order: {
		width: 351 * scale,
	}, conImage: {
		width: 20 * scale,
		height: 14 * scale,
		marginLeft: 5 * scale,
	}, forwardText: {
		paddingTop: 2 * scale,
		marginTop: 2 * scale,
		marginLeft: 6 * scale,
		width: 30 * scale,
		height: 16 * scale,
		backgroundColor: '#49CAFF',
		color: '#FFFFFF',
		fontSize: 11 * scale,
		textAlign: 'center',
		borderRadius: 4 * scale,
	},
	statusText: {
		fontSize: 13 * scale,
		color: style.color.themeColor,
	},
	accountSelect: {
		backgroundColor: style.color.themeColor,
		flexDirection: 'row',
		alignItems: 'center',
		justifyContent: 'center'
	},
	filterWrap: {
		backgroundColor: style.color.themeColor,
		flexDirection: 'row',
		alignItems: 'center',
		justifyContent: 'center',
		position: 'relative',
		paddingLeft: 15,
		paddingRight: 5,
		paddingBottom: 10
	},
	dateWrap: {
		width: 110 * scale,
		height: 36 * scale,
		paddingLeft: 36 * scale,
		borderRadius: 18 * scale,
		// backgroundColor: 'rgba(255 ,255 , 255, .18)',
		flexDirection: 'row',
		justifyContent: 'center',
		paddingRight: 14 * scale,
	},
	dateImgWrap: {
		marginRight: 5,
		flexDirection: 'row',
		justifyContent: 'center',
		alignItems: 'center',
	},
	dateText: {
		fontSize: 10 * scale,
		color: '#fff',
		textAlign: 'center'
	},
	dateLine: {
		width: 7 * scale,
		height: 1 * scale,
		backgroundColor: 'rgba(255, 255, 255, .36)',
		marginTop: 3 * scale,
		marginBottom: 3 * scale,
	},
	searchInput: {
		height: 36 * scale,
		width: 280 * scale,
		textAlign: 'center',
		// paddingLeft: 30 * scale,
		// paddingRight: 12 * scale,
		backgroundColor: 'rgba(255, 255, 255, .4)',
		flexDirection: 'row',
		justifyContent: 'center',
		alignItems: 'center',
		paddingVertical: 0,
		position: 'relative',
		borderRadius: 8 * scale,
		color: '#fff',
	},
	searchIcon: {
		width: 18 * scale,
		height: 18 * scale,
		position: 'absolute',
		left: 15 * scale,
		top: 10 * scale,
		zIndex: 9,
		opacity: 0.8,
	},
	tabWrap: {
		backgroundColor: style.color.bgGray,
		flexDirection: 'row',
		alignItems: 'center',
		paddingTop: 10,
		paddingBottom: 10
	},
	tabItem: {
		// flex:1,
		paddingTop: 5 * scale,
		paddingBottom: 5 * scale,
		paddingLeft: 16 * scale,
		paddingRight: 16 * scale,
		flexDirection: 'row',
		alignItems: 'center',
		marginHorizontal: 10,
		borderWidth: 1,
		borderRadius: 20,
		borderColor: style.color.themeColor
	},
	tabText: {
		color: '#fff'
	},
	numPoint: { backgroundColor: '#fff', width: 5, height: 5, position: 'absolute', borderRadius: 16, right: 10, top: 5 }
})